
#include "user.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// record the user logged in
// free when log out
static User *login_user;

// to load the books files and users files
void load()
{
    load_books();
    load_users();
}

// to store the books and users when exit program
void store()
{
    store_books();
    store_users();
}

// input question: string, get the answer from user's input
char *ask_question(const char *question)
{
	printf("%s",question);// display the question
	const int step = 2;// the size of every steps

    // the first step
	char *answer = malloc(step);
	answer[0] = 0;
    // now it's a string of length 0

	//read until a new line
	char *next_step = answer;
	int times = 0;//the number of steps
	do {
		answer = realloc(answer, step + times * step);
		next_step = answer + strlen(answer);// answer may point to other as realloc()

        // get the string within the size of a step
		fgets(next_step, step, stdin);
		next_step = answer + strlen(answer);// move pointer next_step to next step
		times++;
	} while (*(next_step-1) != '\n');

	*(next_step-1) = 0;//truncate the string eliminating the new line.

	return answer;
}

// disply the question to get the information needed
// quit to main menu at all events
void register_interface()
{
    char *name = ask_question("\nPlease enter your real name:");
    char *username = ask_question("Please enter a username:");
    char *password = ask_question("Please enter a password:");

    // check input valid or not  
    if(match(username)==0)//return 1 if the same name is found
    register_user(name,username, password);//return 0 if register successfully

    free(name);
    free(username);
    free(password);
}

// a new interface with choices to choose how to search books
void searchbooks_interface()
{
    int choice=4;//quit
    do
    {
        char *answer=ask_question("\nPlease choose an option:\n1.Search books by title\n2.Search books by author\n3.Search books by year\n4.Back\nOption:");
        choice = atoi(answer);
        free(answer);

        // select next step, how to search or quit search interface
        switch(choice){

            // search by title
            case 1:
            {char *title = ask_question("\nPlease enter the title:");
            display_books(find_book_by_title (title));
            free(title);}
            break;

            // search by authors
            case 2:
            {char *authors = ask_question("\nPlease enter the authors:");
            display_books(find_book_by_author(authors));
            free(authors);}
            break;

            // search by year
            case 3:
            {char *year_string=ask_question("\nPlease enter the year that the book published:");
            int year=atoi(year_string);
            free(year_string);
            display_books(find_book_by_year(year));
            }
            break;

            // quit
            case 4:
            printf("Cancel searching.\n");
            break;

            default:
            printf("Please choose the options above\n");
        }
    } while (choice!=4);
}

// display the question to get information about the book wish to add
void add_book_interface()
{
    Book book;

    // get title, authors, year and copies
    char *title = ask_question("\nEnter the title of the book:");
    char *authors = ask_question("Enter the author of the book:");
    char *year_string = ask_question("Enter the year:");
    int year = atoi(year_string);
    free(year_string);
    char *copies_string = ask_question("Enter the number of copies:");
    int copies = atoi(copies_string);
    free(copies_string);

    //check year,copies
    if(year<=0||copies<=0){
        printf("Please enter valid values of year and copies.\n");
    }else{

        // add book
        book.title=title;
        book.authors=authors;
        book.year=year;
        book.copies=copies;
        book.loan=0;
        add_book(book);
    }

    free(title);
    free(authors);
}

void remove_book_interface()
{
    display_allbooks();
    char *answer = ask_question("\nPlease enter the Id number of the book you wish to delete:");
    int id = atoi(answer);
    free(answer);
    remove_book(id);
}

void librarian_menu()
{
    int choice=5;//quit
    do
    {
        char *answer=ask_question("\nPlease choose an option:\n1.Add a book\n2.Remove books\n3.Search for books\n4.Display all books\n5.Log out\nOption:");
        choice = atoi(answer);
        free(answer);
        switch(choice){
            case 1://add a book
            add_book_interface();
            break;
            case 2://remove books
            remove_book_interface();
            break;
            case 3://search for books
            searchbooks_interface();
            break;
            case 4://display all books
            display_allbooks();
            break;
            case 5://log out
            printf("Log out.\n");
            break;
            default:
            printf("Please choose the options above\n");
        }
    } while (choice!=5);
}

void borrow_interface()
{
    char *answer=ask_question("\nEnter the ID number of the book you wish to borrow:");
    int id = atoi(answer);
    free(answer);
    borrow_book(id,login_user);
}
void return_interface()
{
    display_books(*login_user->books);
    char *answer=ask_question("\nEnter the ID number of the book you wish to return:");
    int id = atoi(answer);
    free(answer);
    return_book(id,login_user);
}

void users_menu()
{
    int choice=5;//quit
    do
    {
        char *answer=ask_question("\nPlease choose an option:\n1.Borrow a book\n2.Return books\n3.Search for books\n4.Display all books\n5.Log out\nOption:");
        choice = atoi(answer);
        free(answer);
        switch(choice){
            case 1://borrow a book
            borrow_interface();
            break;
            case 2://Return books
            return_interface();
            break;
            case 3:
            searchbooks_interface();
            break;
            case 4:
            display_allbooks();
            break;
            case 5:
            printf("Log out.\n");
            break;
            default:
            printf("Please choose the options above\n");
        }
    } while (choice!=5);
}

void login_interface()
{
    int flag=0;//get the result from verify
 
    char *login_username = ask_question("\nPlease enter your username:");
    char *login_password = ask_question("Please enter your password:");
    login_user=verify(login_username,login_password);
    if(login_user){
        if(!strcmp(login_user->username,"librarian"))
            flag=2;
        else flag=1;
    }
    free(login_username);
    free(login_password);
    if(flag==2)//librarian log in
        librarian_menu();
    if(flag==1)//users log in
        users_menu();
}

// main part of the program
void main_menu(){
    int choice=5;//quit
    do
    {
        char *answer=ask_question("\nPlease choose an option:\n1.Register\n2.Log in\n3.Search for books\n4.Display all books\n5.Quit\nOption:");
        choice = atoi(answer);
        free(answer);

        // choose the next interface depending on choice
        switch(choice){
            case 1:
            register_interface();
            break;
            case 2:
            login_interface();
            break;
            case 3:
            searchbooks_interface();
            break;
            case 4:
            display_allbooks();
            break;

            // quit the program
            case 5:
            printf("Exit program.\n");
            break;

            // invalid input that beyond the cases from above
            default:
            printf("Please choose the options above\n");
        }
    } while (choice!=5);
}

int main()
{
    load();
    main_menu();
    store();
    return 0;
}